#include <strings.h>
#include "CConfig.h"


Config_Base::Config_Base(void)
{
    m_pPath = NULL;

    m_iFlag = -1;

}

Config_Base::~Config_Base(void)
{
    if (m_pPath)
    {
        delete []m_pPath;
    }
}

int Config_Base::Load(const char *pPath,List_Config_Item& listItem)
{
    if (NULL == pPath)
    {
        return -1;
    }

    //如果文件打开失败，返回
    FILE *fp = fopen(pPath, "r+");
    if (NULL == fp)
    {
        return -1;
    }

    listItem.clear();

    //读取配置文件项
    char *p1 = NULL;
    char *p2 = NULL;
    string strItem;
    string strKey;
    string strValue;
    string strTemp;

    char szBuf[256] = {0};
    while (fgets(szBuf, 255, fp) != NULL)
    {
        if ('#' == *szBuf || '\0' == *szBuf)
        {
            continue;
        }

        p1 = strchr(szBuf, '[');
        if (p1 != NULL)
        {
            p2 = strrchr(szBuf, ']');
            if (p2 != NULL)
            {
                strItem.clear();
                copy(p1 + 1, p2, back_inserter(strItem));
            }
            else
            {
                //错误的编辑项，关闭文件并返回错误
                fclose(fp);
                return -1;
            }
        }
        else
        {
            p1 = strchr(szBuf, '=');
            if (p1 != NULL)
            {
                strKey.clear();
                copy(szBuf, p1, back_inserter(strKey));

                //判断strKey里面是否包含注释符
                if (strchr(strKey.c_str(), '#') != NULL)
                {
                    continue;
                }

                //去掉strKey前后的空格和tab键
                strKey.erase(0, strKey.find_first_not_of(" \t\n"));
                strKey.erase(strKey.find_last_not_of(" \t") + 1);

                p2 = strrchr(szBuf, '\n');
                if (NULL == p2)
                {
                    p2 = szBuf + strlen(szBuf);
                }

                strTemp.clear();
                copy(p1 + 1, p2, back_inserter(strTemp));

                //判断值里面是否包含注释符
                p1 = strchr((char *)strTemp.c_str(), '#');
                if (p1 != NULL)
                {
                    strValue.clear();
                    copy((char *)strTemp.c_str(), p1, back_inserter(strValue));
                }
                else
                {
                    strValue = strTemp;
                }

                //去掉值前后的空格和tab键
                strValue.erase(0, strValue.find_first_not_of(" \t\n"));
                strValue.erase(strValue.find_last_not_of(" \t") + 1);

                listItem.push_back(make_pair(strItem, make_pair(strKey, strValue)));
            }
        }
    }

    fclose(fp);

    //如果有路径信息，先判断是否是Reload调用该接口
    if (m_pPath)
    {
        if (strcmp(pPath, m_pPath) == 0)
        {
            return 0;
        }

        delete []m_pPath;
    }

    m_pPath = new char[strlen(pPath) + 1];
    strcpy(m_pPath, pPath);

    return 0;
}

int Config_Base::Load(const char *pPath)
{
    if (m_iFlag == -1)
    {
        int iRet = Load(pPath,m_oldlstConfig);
        if (iRet)
        {
            return iRet;
        }

        iRet = Load(pPath,m_newlstConfig);
        if (iRet)
        {
            return iRet;
        }

        m_iFlag = 0; 
    }
    else
    {
        return 0;
    }

    return 0;
}

int Config_Base::Reload(void)
{
    if (m_iFlag == 0)
    {
        int iRet = Load(m_pPath,m_newlstConfig);
        if (iRet)
        {
            return iRet;
        }

        m_iFlag = 1;
    }
    else if (m_iFlag == 1)
    {
        int iRet = Load(m_pPath,m_newlstConfig);
        if (iRet)
        {
            return iRet;
        }

        m_iFlag = 0;
    }

    return -1;
}

//获取某个配置项子项目的值
int Config_Base::Read(const string &strItem, const string &strLvalue, string &strValue)
{
    List_Config_Item::iterator iter;

    List_Config_Item* plstConfig = NULL;
    if (m_iFlag == 0)
    {
        plstConfig = &m_oldlstConfig;
    }
    else if (m_iFlag == 1)
    {
        plstConfig = &m_newlstConfig;
    }
    else
    {
        return -1;
    }

    for (iter = plstConfig->begin(); iter != plstConfig->end(); ++iter)
    {
        if (strItem == iter->first)
        {
            if (strLvalue == iter->second.first)
            {
                strValue = iter->second.second;
                return 0;
            }
        }
    }

    strValue = "";

    return -1;
}

//获取某个配置项内容
int Config_Base::Get(string &strItem, string &strLvalue, string &strValue)
{
    List_Config_Item::iterator iter;

    List_Config_Item* plstConfig = NULL;
    if (m_iFlag == 0)
    {
        plstConfig = &m_oldlstConfig;
    }
    else if (m_iFlag == 1)
    {
        plstConfig = &m_newlstConfig;
    }
    else
    {
        return -1;
    }

    for (iter = plstConfig->begin(); iter != plstConfig->end(); ++iter)
    {
        if (strItem.empty())
        {
            if (strLvalue.empty() || strValue.empty())
            {
                return -1;
            }

            if (strLvalue == iter->second.first && strValue == iter->second.second)
            {
                strItem = iter->first;
                return 0;
            }
        }
        else if (strLvalue.empty())
        {
            if (strItem.empty() || strValue.empty())
            {
                return -1;
            }

            if (strItem == iter->first && strValue == iter->second.second)
            {
                strLvalue = iter->second.first;
                return 0;
            }
        }
        else
        {
            if (strItem.empty() || strLvalue.empty())
            {
                return -1;
            }

            if (strItem == iter->first && strLvalue == iter->second.first)
            {
                strValue = iter->second.second;
                return 0;
            }
        }
    }

    return -1;
}
